/*
 * Created on 8/Fev/2005
 */
package edu.mapi.ir.gui;

import java.awt.Insets;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.JButton;

/**
 * A WindowsXP style button (the margins only appear when the mouse is over it)
 * Also has a convenient contructor that loads an image an sets the tooltiptext for you. 
 * @author ZP
 */
public class ToolbarButton extends JButton{

	public static final long serialVersionUID = 17;

	
	/**
	 * The class contructor
	 * @param imageURL The url of the icon to be displayed on the button
	 * @param toolTipText The text shown to the user when the mouse is over it
	 * @param actionCommand The action to be fired when the user clicks this button
	 * @param cl The ClassLoader that will load the image file
	 */
	public ToolbarButton(String imageURL, String toolTipText, String actionCommand) {
		super(new ImageIcon(GuiUtils.getImage(imageURL)));
		
		setToolTipText(toolTipText);
		setActionCommand(actionCommand);
		setBorderPainted(false);
		setMargin(new Insets(1,1,1,1));
		addMouseListener(new MouseAdapter() {
			public void mouseEntered(MouseEvent arg0) {
				if (isEnabled()) {
					setBorderPainted(true);
					repaint();
				}
			}
			
			public void mouseExited(MouseEvent arg0) {
				setBorderPainted(false);
				repaint();
			}
		});
	}
	
	/**
	 * The class contructor
	 * @param imageURL The url of the icon to be displayed on the button
	 * @param toolTipText The text shown to the user when the mouse is over it
	 * @param actionCommand The action to be fired when the user clicks this button
	 * @param cl The ClassLoader that will load the image file
	 */
	public ToolbarButton(ImageIcon image, String toolTipText, String actionCommand) {
		super(image);
		
		setToolTipText(toolTipText);
		setActionCommand(actionCommand);
		setBorderPainted(false);
		setMargin(new Insets(1,1,1,1));
		addMouseListener(new MouseAdapter() {
			public void mouseEntered(MouseEvent arg0) {
				if (isEnabled()) {
					setBorderPainted(true);
					repaint();
				}
			}
			
			public void mouseExited(MouseEvent arg0) {
				setBorderPainted(false);
				repaint();
			}
		});
	}
	
	public ToolbarButton(AbstractAction action) {
		super(action);
		setText("");
		setBorderPainted(false);
		setMargin(new Insets(1,1,1,1));
		addMouseListener(new MouseAdapter() {
			public void mouseEntered(MouseEvent arg0) {
				if (isEnabled()) {
					setBorderPainted(true);
					repaint();
				}
			}
			
			public void mouseExited(MouseEvent arg0) {
				setBorderPainted(false);
				repaint();
			}
		});
		
		if (action.getValue(AbstractAction.SHORT_DESCRIPTION) != null) {
			setToolTipText(action.getValue(AbstractAction.SHORT_DESCRIPTION).toString());
		}
	}
}
